﻿#include "bullet.h"
#include<QtMath>

#define GUN_RADIUS 150
const double circleRatio=3.14;

bullet::bullet(const QString&path,QGraphicsScene*scene,qreal angle):pixmapItem(path,scene)
{
    qreal ratio=angle*circleRatio/180;//角度转换为弧度
    setPos(mapToScene(scene->width()/2+GUN_RADIUS*sin(ratio),scene->height()-GUN_RADIUS*cos(ratio)));//子弹正对大炮口
    setRotation(angle);
}

void bullet::advance(int phase)
{
    if(mapToScene(0,0).x()<0||mapToScene(0,0).x()>scene()->width()||mapToScene(0,0).y()<0)
    {
        delete this;
    }
    else
    {
        setPos(mapToScene(0,-7));//朝射出去的方向移动
    }
}

void bullet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(QPoint(-pixmap.width()/2,-pixmap.height()),pixmap);
    if(!this->collidingItems().empty())//碰撞检测
    {
        net *n=new net("./net.jpeg",scene());
        n->setPos(mapToScene(0,0));
        delete this;
    }
}
